package com.lin.zoo.view;

import com.lin.zoo.dao.UserDao;
import com.lin.zoo.po.Messege;
import com.lin.zoo.util.DataBase;
import com.lin.zoo.controller.IndentController;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.Vector;

public class IndentVisitorPage {
    private JFrame frame=new JFrame("门票订单系统");
    private JButton addButton,delButton;
    private JTable jTable;
    private DefaultTableModel mol;
    private JScrollPane scrollPane;
    private JPanel panel;
    private Vector<String> rowVector;
    private Vector<Vector<String>> dataVector;
    private Connection con;
    private PreparedStatement state;
    private ResultSet rs;
    private IndentController indentController;

    public IndentController getIndentController() {
        return indentController;
    }

    public void setIndentController(IndentController indentController) {
        this.indentController = indentController;
    }

    public JButton getAddButton() {
        return addButton;
    }

    public void setAddButton(JButton addButton) {
        this.addButton = addButton;
    }

    public JButton getDelButton() {
        return delButton;
    }

    public void setDelButton(JButton delButton) {
        this.delButton = delButton;
    }

    public void createAndShowGUI() throws SQLException{
        setIndentController(new IndentController());
        frame.setVisible(true);
        frame.setSize(1000,400);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        // 显示窗口
        frame.setResizable(false);
        //固定窗口大小不被修改
        frame.setLocationRelativeTo(null);
        //窗口处于中央
        rowVector= new Vector<>();
        rowVector.add("id");
        rowVector.add("订单类型");
        rowVector.add("下单时间");
        rowVector.add("有效期");
        rowVector.add("游客编号");
        rowVector.add("是否申请退票");
        rowVector.add("金额");
        mol = new DefaultTableModel();
        try {
            con =DataBase.getCon();
            state = con.prepareStatement("select * from indent where visitorid=?");
            state.setObject(1, UserDao.getLoginId());
            rs=state.executeQuery();
        } catch (ClassNotFoundException e) {
            System.out.println("数据读取错误" + this.getClass().getName());
        }//直接在view中调用了数据库以查询表格数据
        setData(rs);
        DataBase.close(state, con);
        jTable=new JTable(mol);
        jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        scrollPane=new JScrollPane(jTable);
        jTable.setRowHeight(50);
        frame.add(scrollPane,BorderLayout.CENTER);//添加滚轮
        init();
        frame.add(panel,BorderLayout.SOUTH);

        //购买门票按钮
        getAddButton().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Messege indent=getIndentController().addIndent();
                JOptionPane.showMessageDialog(null, indent.getResult(), null, JOptionPane.PLAIN_MESSAGE);
                frame.setVisible(false);
                try {
                   IndentVisitorPage indentVisitorPage=new IndentVisitorPage();
                   indentVisitorPage.createAndShowGUI();
                } catch (SQLException ex) {
                    System.out.println("购买失败" + this.getClass().getName());
                }
            }
        });
        //退票按钮
        getDelButton().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int seletedRow=jTable.getSelectedRow();
                seletedRow=jTable.convertRowIndexToModel(seletedRow);
                Object id=jTable.getValueAt(seletedRow+0,0);
                jTable.setValueAt("是",seletedRow+0,5);
                System.out.println(id);
                Messege cancel = indentController.delindent(id);
                JOptionPane.showMessageDialog(null, cancel.getResult(), "入园票", JOptionPane.PLAIN_MESSAGE);
            }
        });
    }
    //页面分布
    private void init(){
        addButton=new JButton("购买门票");
        delButton=new JButton("退票申请");
        panel=new JPanel();

        panel.setLayout(new GridLayout(1,1));

        panel.add(addButton);
        panel.add(delButton);
    }
    //读取数据
    private void setData(ResultSet rs) throws SQLException{
        dataVector = new Vector<>();
        while(rs.next()) {
            Vector<String> vector = new Vector<>();
            for (int i = 1; i <= 7; i++)//7代表列数
                vector.add(rs.getString(i));//添加数据库数据到vector
            dataVector.add(vector);//添加vector数据到dataVector
        }
        mol.setDataVector(dataVector,rowVector);//最后将数据输入至mol
    }
}
